library(readxl)
library(haven)
library(tidyverse)
library(ggplot2)

### PURPOSE:
#	- to create Figures 1a and 1b in King, Schoenherr, and Ostrander

#################################
### STEP 1: READ IN THE FILES ###
#################################

districtData <- read_excel("DistrictCourtRunningTally20231110.xlsx")

circuitData <- read_excel("CircuitCourtRunningTally20231110.xlsx")

########################################
### STEP 2: PLOT DISTRICT COURT DATA ###
########################################

# goal: one plot with two separate lines
# 	- one line for cumulative number of courts that have, at some point, had a woman
# 	- one line for how many courts in a given year had at least one woman
# note: the first line should always be higher than the second line

# these are percentages, so y axis runs from 0 to 1
# district court graph runs from 1961 to 2023 - break for each admin


districtPlot <- ggplot(districtData, aes(x = year)) +
	geom_rect(aes(xmin = 1969, xmax = 1977, ymin = 0, ymax = Inf), fill = "grey90", alpha = 0.05) +
	geom_rect(aes(xmin = 1981, xmax = 1989, ymin = 0, ymax = Inf), fill = "grey90", alpha = 0.05) +
	geom_rect(aes(xmin = 1993, xmax = 2001, ymin = 0, ymax = Inf), fill = "grey90", alpha = 0.05) +
	geom_rect(aes(xmin = 2009, xmax = 2017, ymin = 0, ymax = Inf), fill = "grey90", alpha = 0.05) +
	geom_rect(aes(xmin = 2021, xmax = 2023, ymin = 0, ymax = Inf), fill = "grey90", alpha = 0.05) +
	geom_line(aes(y = percentDistrictsYear, color = "Courts With at Least One Female Judge That Year", linetype = "Courts With at Least One Female Judge That Year")) +	
	geom_line(aes(y = percentDistrictsCumulative, color = "Cumulative Courts That Have Had a Female Judge", linetype = "Cumulative Courts That Have Had a Female Judge")) +
	theme_bw() +
	theme(panel.grid.minor = element_blank()) + 
	theme(legend.position = "bottom", legend.title = element_blank()) +
	labs(x = "\nYear", y = "\nProportion of District Courts\n", linetype = "variable", color = "variable") +
	scale_color_manual(breaks = c("Courts With at Least One Female Judge That Year", "Cumulative Courts That Have Had a Female Judge"), values = c("Courts With at Least One Female Judge That Year" = "grey50", "Cumulative Courts That Have Had a Female Judge" = "black")) +
	scale_linetype_manual(breaks = c("Courts With at Least One Female Judge That Year", "Cumulative Courts That Have Had a Female Judge"), values = c("Courts With at Least One Female Judge That Year" = "dashed", "Cumulative Courts That Have Had a Female Judge" = "solid")) +
	scale_x_continuous(limits = c(1961, 2023), breaks = seq(1961, 2023, 4), expand = c(0,0)) +
	scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, 0.2), expand = c(0, 0)) + 
	theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) + 
	ggtitle("Inclusion of Women on District Courts Over Time\n") +
	theme(plot.title = element_text(hjust = 0.5)) +
	theme(plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm"))
districtPlot

#######################################
### STEP 3: PLOT CIRCUIT COURT DATA ###
#######################################

# goal: one plot with two separate lines
# 	- one line for cumulative number of courts that have, at some point, had a woman
# 	- one line for how many courts in a given year had at least one woman
# note: the first line should always be higher than the second line

# these are percentages, so y axis runs from 0 to 1
# circuit court graph runs from 1933 to 2023 - break for each presidential admin

circuitPlot <- ggplot(circuitData, aes(x = year)) +
	geom_rect(aes(xmin = 1933, xmax = 1945, ymin = 0, ymax = Inf), fill = "grey90", alpha = 0.05) +
	geom_rect(aes(xmin = 1953, xmax = 1961, ymin = 0, ymax = Inf), fill = "grey90", alpha = 0.05) +
	geom_rect(aes(xmin = 1969, xmax = 1977, ymin = 0, ymax = Inf), fill = "grey90", alpha = 0.05) +
	geom_rect(aes(xmin = 1981, xmax = 1989, ymin = 0, ymax = Inf), fill = "grey90", alpha = 0.05) +
	geom_rect(aes(xmin = 1993, xmax = 2001, ymin = 0, ymax = Inf), fill = "grey90", alpha = 0.05) +
	geom_rect(aes(xmin = 2009, xmax = 2017, ymin = 0, ymax = Inf), fill = "grey90", alpha = 0.05) +
	geom_rect(aes(xmin = 2021, xmax = 2023, ymin = 0, ymax = Inf), fill = "grey90", alpha = 0.05) +
	geom_line(aes(y = percentCircuitsYear, color = "Courts With at Least One Female Judge That Year", linetype = "Courts With at Least One Female Judge That Year")) +	
	geom_line(aes(y = percentCircuitsCumulative, color = "Cumulative Courts That Have Had a Female Judge", linetype = "Cumulative Courts That Have Had a Female Judge")) +
	theme_bw() +
	theme(panel.grid.minor = element_blank()) + 
	theme(legend.position = "bottom", legend.title = element_blank()) +
	labs(x = "\nYear", y = "\nProportion of Circuit Courts\n", linetype = "variable", color = "variable") +
	scale_color_manual(breaks = c("Courts With at Least One Female Judge That Year", "Cumulative Courts That Have Had a Female Judge"), values = c("Courts With at Least One Female Judge That Year" = "grey50", "Cumulative Courts That Have Had a Female Judge" = "black")) +
	scale_linetype_manual(breaks = c("Courts With at Least One Female Judge That Year", "Cumulative Courts That Have Had a Female Judge"), values = c("Courts With at Least One Female Judge That Year" = "dashed", "Cumulative Courts That Have Had a Female Judge" = "solid")) +
	scale_x_continuous(limits = c(1933, 2023), breaks = seq(1933, 2023, 4), expand = c(0,0)) +
	scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, 0.2), expand = c(0, 0.01)) + 
	theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) + 
	ggtitle("Inclusion of Women on Circuit Courts Over Time\n") +
	theme(plot.title = element_text(hjust = 0.5)) +
	theme(plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm"))
circuitPlot



